package com.zjl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjl.mapper.CategoryMapper;
import com.zjl.pojo.Category;
import com.zjl.pojo.Dish;
import com.zjl.pojo.SetMeal;
import com.zjl.service.CategoryService;
import com.zjl.service.DishService;
import com.zjl.service.SetMealService;
import com.zjl.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> DishQueryWrapper = new LambdaQueryWrapper<>();
        DishQueryWrapper.eq(Dish::getCategoryId, id);
        Long count1 = dishService.count(DishQueryWrapper);
        if(count1>0) {
            // 抛出异常
            throw new CustomException("当前分类关联菜品，不能删除！");
        }

        LambdaQueryWrapper<SetMeal> SetMealQueryWrapper = new LambdaQueryWrapper<>();
        SetMealQueryWrapper.eq(SetMeal::getCategoryId, id);
        Long count2 = setMealService.count(SetMealQueryWrapper);
        if(count2>0) {
            // 抛出异常
            throw new CustomException("当前分类关联套餐，不能删除！");
        }

//        categoryMapper.deleteById(id);
        super.removeById(id);
    }
}
